//
//  HNActorInfoCache.m
//  HeroNuts
//
//  Created by Magic on 4/4/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "HNActorInfoCache.h"
#import "CJSONDeserializer.h"

@implementation HNActorInfoCache

#pragma mark HNActorInfoCache - Alloc, Init & Dealloc

static HNActorInfoCache* sharedActorInfoCache_ = nil;

+ (HNActorInfoCache*)sharedActorInfoCache
{
	if (!sharedActorInfoCache_)
	sharedActorInfoCache_ = [[HNActorInfoCache alloc] init];
	                 
	return sharedActorInfoCache_;
}

+(id)alloc
{
	NSAssert(sharedActorInfoCache_ == nil, @"Attempted to allocate a second instance of a singleton.");
	return [super alloc];
}


-(id) init
{
	if( (self = [super init]) ) 
	{
		actorInfo_ = [[NSMutableArray alloc] initWithCapacity: 2];
	}
	return self;	
}

-(void) dealloc
{	
	[actorInfo_ release];	
	[super dealloc];
}

#pragma mark HNActorInfoCache - loading actor info

-(void) addActorInfoWithFile:(NSString*)file
{
	NSString *filePath; 

	filePath = [[NSBundle mainBundle] pathForResource:file ofType:@""];
	
	NSData *jsonData = [[NSFileManager defaultManager] contentsAtPath:filePath];	
	
	CJSONDeserializer *jsonDeserializer = [CJSONDeserializer deserializer];
	NSError *error = nil;
	NSDictionary *actorsOfATown = [jsonDeserializer deserializeAsDictionary:jsonData error:&error];
	if (error) {
		//handle Error, didn't have here.
	}
	
	[actorInfo_ addObject:actorsOfATown];
}

#pragma mark HNActorInfoCache - getting

- (NSDictionary*)actorInfoByName:(NSString*)name
{
	for (NSDictionary* dic in actorInfo_) 
	{
		NSDictionary* result = [dic objectForKey:name];
		if (result) {
			
			return result;
		}
	}
	
	return nil;	
}

- (CGFloat)faceLeftOffsetXByActor:(NSString*)name
{
	NSDictionary* info = [self actorInfoByName:name];
	NSNumber* faceLeftOffsetX= [info valueForKey:@"FaceLeftOffsetX"];
	CGFloat offsetX = [faceLeftOffsetX floatValue];
	return offsetX;
}

- (CGPoint)originalPositionByActor:(NSString*)name
{
	NSDictionary* info = [self actorInfoByName:name];
	NSDictionary* original_position = [info valueForKey:@"OriginalPosition"];
	NSNumber* x_position = [original_position valueForKey:@"x"];
	NSNumber* y_position = [original_position valueForKey:@"y"];
	CGFloat x = [x_position floatValue];
	CGFloat y = [y_position floatValue];
	return CGPointMake(x,y);
}

- (NSArray*)actionFramesByName:(NSString*)action actor:(NSString*)actor
{
	NSDictionary* info = [self actorInfoByName:actor];
	NSArray* result = [info valueForKey:action];
	return result;
}

- (CGPoint)troopCountOffsetByActor:(NSString*)actor
{
    NSDictionary* info = [self actorInfoByName:actor];
    NSDictionary* troopCountOffset = [info valueForKey:@"TroopCountOffset"];
	NSNumber* x_position = [troopCountOffset valueForKey:@"x"];
	NSNumber* y_position = [troopCountOffset valueForKey:@"y"];
	CGFloat x = [x_position floatValue];
	CGFloat y = [y_position floatValue];
	return CGPointMake(x,y);
}

- (CGFloat)frameDelayByActor:(NSString*)actor
{
	NSDictionary* info = [self actorInfoByName:actor];
	NSNumber* delay = [info valueForKey:@"FrameDelay"];
	return [delay floatValue];
	
}

+(void) purgeSharedActorInfoCache
{
	[sharedActorInfoCache_ release];
	sharedActorInfoCache_ = nil;
}



@end
